package _interview150;

/**
 * 13. 罗马数字转整数
 */
public class No13 {
    public int romanToInt(String s) {
        int ans = getI(s.charAt(s.length() - 1));
        for (int j = s.length() - 2; j >= 0; j--) {
            int cur = getI(s.charAt(j));
            int pre = getI(s.charAt(j + 1));
            if (cur >= pre) ans += cur;
            else ans -= cur;
        }
        return ans;
    }

    private int getI(char c) {
        return switch (c) {
            case 'I' -> 1;
            case 'V' -> 5;
            case 'X' -> 10;
            case 'L' -> 50;
            case 'C' -> 100;
            case 'D' -> 500;
            case 'M' -> 1000;
            default -> 0;
        };
    }
}
